home *** CD-ROM | disk | FTP | other *** search
/ Ultra Pack / UltraComputing Partner Applications.iso / SunLabs / tclTK / src / tk4.0 / tkListbox.c < prev    next >
C/C++ Source or Header  |  1995-06-23  |  68KB  |  2,228 lines

  1. /* 
  2.  * tkListbox.c --
  3.  *
  4.  *    This module implements listbox widgets for the Tk
  5.  *    toolkit.  A listbox displays a collection of strings,
  6.  *    one per line, and provides scrolling and selection.
  7.  *
  8.  * Copyright (c) 1990-1994 The Regents of the University of California.
  9.  * Copyright (c) 1994-1995 Sun Microsystems, Inc.
  10.  *
  11.  * See the file "license.terms" for information on usage and redistribution
  12.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  13.  */
  14.  
  15. static char sccsid[] = "@(#) tkListbox.c 1.96 95/06/23 08:46:14";
  16.  
  17. #include "tkPort.h"
  18. #include "default.h"
  19. #include "tkInt.h"
  20.  
  21. /*
  22.  * One record of the following type is kept for each element
  23.  * associated with a listbox widget:
  24.  */
  25.  
  26. typedef struct Element {
  27.     int textLength;        /* # non-NULL characters in text. */
  28.     int lBearing;        /* Distance from first character's
  29.                  * origin to left edge of character. */
  30.     int pixelWidth;        /* Total width of element in pixels (including
  31.                  * left bearing and right bearing). */
  32.     int selected;        /* 1 means this item is selected, 0 means
  33.                  * it isn't. */
  34.     struct Element *nextPtr;    /* Next in list of all elements of this
  35.                  * listbox, or NULL for last element. */
  36.     char text[4];        /* Characters of this element, NULL-
  37.                  * terminated.  The actual space allocated
  38.                  * here will be as large as needed (> 4,
  39.                  * most likely).  Must be the last field
  40.                  * of the record. */
  41. } Element;
  42.  
  43. #define ElementSize(stringLength) \
  44.     ((unsigned) (sizeof(Element) - 3 + stringLength))
  45.  
  46. /*
  47.  * A data structure of the following type is kept for each listbox
  48.  * widget managed by this file:
  49.  */
  50.  
  51. typedef struct {
  52.     Tk_Window tkwin;        /* Window that embodies the listbox.  NULL
  53.                  * means that the window has been destroyed
  54.                  * but the data structures haven't yet been
  55.                  * cleaned up.*/
  56.     Display *display;        /* Display containing widget.  Used, among
  57.                  * other things, so that resources can be
  58.                  * freed even after tkwin has gone away. */
  59.     Tcl_Interp *interp;        /* Interpreter associated with listbox. */
  60.     Tcl_Command widgetCmd;    /* Token for listbox's widget command. */
  61.     int numElements;        /* Total number of elements in this listbox. */
  62.     Element *firstPtr;        /* First in list of elements (NULL if no
  63.                  * elements). */
  64.     Element *lastPtr;        /* Last in list of elements (NULL if no
  65.                  * elements). */
  66.  
  67.     /*
  68.      * Information used when displaying widget:
  69.      */
  70.  
  71.     Tk_3DBorder normalBorder;    /* Used for drawing border around whole
  72.                  * window, plus used for background. */
  73.     int borderWidth;        /* Width of 3-D border around window. */
  74.     int relief;            /* 3-D effect: TK_RELIEF_RAISED, etc. */
  75.     int highlightWidth;        /* Width in pixels of highlight to draw
  76.                  * around widget when it has the focus.
  77.                  * <= 0 means don't draw a highlight. */
  78.     XColor *highlightBgColorPtr;
  79.                 /* Color for drawing traversal highlight
  80.                  * area when highlight is off. */
  81.     XColor *highlightColorPtr;    /* Color for drawing traversal highlight. */
  82.     int inset;            /* Total width of all borders, including
  83.                  * traversal highlight and 3-D border.
  84.                  * Indicates how much interior stuff must
  85.                  * be offset from outside edges to leave
  86.                  * room for borders. */
  87.     XFontStruct *fontPtr;    /* Information about text font, or NULL. */
  88.     XColor *fgColorPtr;        /* Text color in normal mode. */
  89.     GC textGC;            /* For drawing normal text. */
  90.     Tk_3DBorder selBorder;    /* Borders and backgrounds for selected
  91.                  * elements. */
  92.     int selBorderWidth;        /* Width of border around selection. */
  93.     XColor *selFgColorPtr;    /* Foreground color for selected elements. */
  94.     GC selTextGC;        /* For drawing selected text. */
  95.     int width;            /* Desired width of window, in characters. */
  96.     int height;            /* Desired height of window, in lines. */
  97.     int lineHeight;        /* Number of pixels allocated for each line
  98.                  * in display. */
  99.     int topIndex;        /* Index of top-most element visible in
  100.                  * window. */
  101.     int fullLines;        /* Number of lines that fit are completely
  102.                  * visible in window.  There may be one
  103.                  * additional line at the bottom that is
  104.                  * partially visible. */
  105.     int partialLine;        /* 0 means that the window holds exactly
  106.                  * fullLines lines.  1 means that there is
  107.                  * one additional line that is partially
  108.                  * visble. */
  109.     int setGrid;        /* Non-zero means pass gridding information
  110.                  * to window manager. */
  111.  
  112.     /*
  113.      * Information to support horizontal scrolling:
  114.      */
  115.  
  116.     int maxWidth;        /* Width (in pixels) of widest string in
  117.                  * listbox. */
  118.     int xScrollUnit;        /* Number of pixels in one "unit" for
  119.                  * horizontal scrolling (window scrolls
  120.                  * horizontally in increments of this size).
  121.                  * This is an average character size. */
  122.     int xOffset;        /* The left edge of each string in the
  123.                  * listbox is offset to the left by this
  124.                  * many pixels (0 means no offset, positive
  125.                  * means there is an offset). */
  126.  
  127.     /*
  128.      * Information about what's selected or active, if any.
  129.      */
  130.  
  131.     Tk_Uid selectMode;        /* Selection style: single, browse, multiple,
  132.                  * or extended.  This value isn't used in C
  133.                  * code, but the Tcl bindings use it. */
  134.     int numSelected;        /* Number of elements currently selected. */
  135.     int selectAnchor;        /* Fixed end of selection (i.e. element
  136.                  * at which selection was started.) */
  137.     int exportSelection;    /* Non-zero means tie internal listbox
  138.                  * to X selection. */
  139.     int active;            /* Index of "active" element (the one that
  140.                  * has been selected by keyboard traversal).
  141.                  * -1 means none. */
  142.  
  143.     /*
  144.      * Information for scanning:
  145.      */
  146.  
  147.     int scanMarkX;        /* X-position at which scan started (e.g.
  148.                  * button was pressed here). */
  149.     int scanMarkY;        /* Y-position at which scan started (e.g.
  150.                  * button was pressed here). */
  151.     int scanMarkXOffset;    /* Value of "xOffset" field when scan
  152.                  * started. */
  153.     int scanMarkYIndex;        /* Index of line that was at top of window
  154.                  * when scan started. */
  155.  
  156.     /*
  157.      * Miscellaneous information:
  158.      */
  159.  
  160.     Cursor cursor;        /* Current cursor for window, or None. */
  161.     char *takeFocus;        /* Value of -takefocus option;  not used in
  162.                  * the C code, but used by keyboard traversal
  163.                  * scripts.  Malloc'ed, but may be NULL. */
  164.     char *yScrollCmd;        /* Command prefix for communicating with
  165.                  * vertical scrollbar.  NULL means no command
  166.                  * to issue.  Malloc'ed. */
  167.     char *xScrollCmd;        /* Command prefix for communicating with
  168.                  * horizontal scrollbar.  NULL means no command
  169.                  * to issue.  Malloc'ed. */
  170.     int flags;            /* Various flag bits:  see below for
  171.                  * definitions. */
  172. } Listbox;
  173.  
  174. /*
  175.  * Flag bits for listboxes:
  176.  *
  177.  * REDRAW_PENDING:        Non-zero means a DoWhenIdle handler
  178.  *                has already been queued to redraw
  179.  *                this window.
  180.  * UPDATE_V_SCROLLBAR:        Non-zero means vertical scrollbar needs
  181.  *                to be updated.
  182.  * UPDATE_H_SCROLLBAR:        Non-zero means horizontal scrollbar needs
  183.  *                to be updated.
  184.  * GOT_FOCUS:            Non-zero means this widget currently
  185.  *                has the input focus.
  186.  */
  187.  
  188. #define REDRAW_PENDING        1
  189. #define UPDATE_V_SCROLLBAR    2
  190. #define UPDATE_H_SCROLLBAR    4
  191. #define GOT_FOCUS        8
  192.  
  193. /*
  194.  * Information used for argv parsing:
  195.  */
  196.  
  197. static Tk_ConfigSpec configSpecs[] = {
  198.     {TK_CONFIG_BORDER, "-background", "background", "Background",
  199.     DEF_LISTBOX_BG_COLOR, Tk_Offset(Listbox, normalBorder),
  200.     TK_CONFIG_COLOR_ONLY},
  201.     {TK_CONFIG_BORDER, "-background", "background", "Background",
  202.     DEF_LISTBOX_BG_MONO, Tk_Offset(Listbox, normalBorder),
  203.     TK_CONFIG_MONO_ONLY},
  204.     {TK_CONFIG_SYNONYM, "-bd", "borderWidth", (char *) NULL,
  205.     (char *) NULL, 0, 0},
  206.     {TK_CONFIG_SYNONYM, "-bg", "background", (char *) NULL,
  207.     (char *) NULL, 0, 0},
  208.     {TK_CONFIG_PIXELS, "-borderwidth", "borderWidth", "BorderWidth",
  209.     DEF_LISTBOX_BORDER_WIDTH, Tk_Offset(Listbox, borderWidth), 0},
  210.     {TK_CONFIG_ACTIVE_CURSOR, "-cursor", "cursor", "Cursor",
  211.     DEF_LISTBOX_CURSOR, Tk_Offset(Listbox, cursor), TK_CONFIG_NULL_OK},
  212.     {TK_CONFIG_BOOLEAN, "-exportselection", "exportSelection",
  213.     "ExportSelection", DEF_LISTBOX_EXPORT_SELECTION,
  214.     Tk_Offset(Listbox, exportSelection), 0},
  215.     {TK_CONFIG_SYNONYM, "-fg", "foreground", (char *) NULL,
  216.     (char *) NULL, 0, 0},
  217.     {TK_CONFIG_FONT, "-font", "font", "Font",
  218.     DEF_LISTBOX_FONT, Tk_Offset(Listbox, fontPtr), 0},
  219.     {TK_CONFIG_COLOR, "-foreground", "foreground", "Foreground",
  220.     DEF_LISTBOX_FG, Tk_Offset(Listbox, fgColorPtr), 0},
  221.     {TK_CONFIG_INT, "-height", "height", "Height",
  222.     DEF_LISTBOX_HEIGHT, Tk_Offset(Listbox, height), 0},
  223.     {TK_CONFIG_COLOR, "-highlightbackground", "highlightBackground",
  224.     "HighlightBackground", DEF_LISTBOX_HIGHLIGHT_BG,
  225.     Tk_Offset(Listbox, highlightBgColorPtr), 0},
  226.     {TK_CONFIG_COLOR, "-highlightcolor", "highlightColor", "HighlightColor",
  227.     DEF_LISTBOX_HIGHLIGHT, Tk_Offset(Listbox, highlightColorPtr), 0},
  228.     {TK_CONFIG_PIXELS, "-highlightthickness", "highlightThickness",
  229.     "HighlightThickness",
  230.     DEF_LISTBOX_HIGHLIGHT_WIDTH, Tk_Offset(Listbox, highlightWidth), 0},
  231.     {TK_CONFIG_RELIEF, "-relief", "relief", "Relief",
  232.     DEF_LISTBOX_RELIEF, Tk_Offset(Listbox, relief), 0},
  233.     {TK_CONFIG_BORDER, "-selectbackground", "selectBackground", "Foreground",
  234.     DEF_LISTBOX_SELECT_COLOR, Tk_Offset(Listbox, selBorder),
  235.     TK_CONFIG_COLOR_ONLY},
  236.     {TK_CONFIG_BORDER, "-selectbackground", "selectBackground", "Foreground",
  237.     DEF_LISTBOX_SELECT_MONO, Tk_Offset(Listbox, selBorder),
  238.     TK_CONFIG_MONO_ONLY},
  239.     {TK_CONFIG_PIXELS, "-selectborderwidth", "selectBorderWidth", "BorderWidth",
  240.     DEF_LISTBOX_SELECT_BD, Tk_Offset(Listbox, selBorderWidth), 0},
  241.     {TK_CONFIG_COLOR, "-selectforeground", "selectForeground", "Background",
  242.     DEF_LISTBOX_SELECT_FG_COLOR, Tk_Offset(Listbox, selFgColorPtr),
  243.     TK_CONFIG_COLOR_ONLY},
  244.     {TK_CONFIG_COLOR, "-selectforeground", "selectForeground", "Background",
  245.     DEF_LISTBOX_SELECT_FG_MONO, Tk_Offset(Listbox, selFgColorPtr),
  246.     TK_CONFIG_MONO_ONLY},
  247.     {TK_CONFIG_UID, "-selectmode", "selectMode", "SelectMode",
  248.     DEF_LISTBOX_SELECT_MODE, Tk_Offset(Listbox, selectMode), 0},
  249.     {TK_CONFIG_BOOLEAN, "-setgrid", "setGrid", "SetGrid",
  250.     DEF_LISTBOX_SET_GRID, Tk_Offset(Listbox, setGrid), 0},
  251.     {TK_CONFIG_STRING, "-takefocus", "takeFocus", "TakeFocus",
  252.     DEF_LISTBOX_TAKE_FOCUS, Tk_Offset(Listbox, takeFocus),
  253.     TK_CONFIG_NULL_OK},
  254.     {TK_CONFIG_INT, "-width", "width", "Width",
  255.     DEF_LISTBOX_WIDTH, Tk_Offset(Listbox, width), 0},
  256.     {TK_CONFIG_STRING, "-xscrollcommand", "xScrollCommand", "ScrollCommand",
  257.     DEF_LISTBOX_SCROLL_COMMAND, Tk_Offset(Listbox, xScrollCmd),
  258.     TK_CONFIG_NULL_OK},
  259.     {TK_CONFIG_STRING, "-yscrollcommand", "yScrollCommand", "ScrollCommand",
  260.     DEF_LISTBOX_SCROLL_COMMAND, Tk_Offset(Listbox, yScrollCmd),
  261.     TK_CONFIG_NULL_OK},
  262.     {TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL,
  263.     (char *) NULL, 0, 0}
  264. };
  265.  
  266. /*
  267.  * Forward declarations for procedures defined later in this file:
  268.  */
  269.  
  270. static void        ChangeListboxOffset _ANSI_ARGS_((Listbox *listPtr,
  271.                 int offset));
  272. static void        ChangeListboxView _ANSI_ARGS_((Listbox *listPtr,
  273.                 int index));
  274. static int        ConfigureListbox _ANSI_ARGS_((Tcl_Interp *interp,
  275.                 Listbox *listPtr, int argc, char **argv,
  276.                 int flags));
  277. static void        DeleteEls _ANSI_ARGS_((Listbox *listPtr, int first,
  278.                 int last));
  279. static void        DestroyListbox _ANSI_ARGS_((ClientData clientData));
  280. static void        DisplayListbox _ANSI_ARGS_((ClientData clientData));
  281. static int        GetListboxIndex _ANSI_ARGS_((Tcl_Interp *interp,
  282.                 Listbox *listPtr, char *string, int numElsOK,
  283.                 int *indexPtr));
  284. static void        InsertEls _ANSI_ARGS_((Listbox *listPtr, int index,
  285.                 int argc, char **argv));
  286. static void        ListboxCmdDeletedProc _ANSI_ARGS_((
  287.                 ClientData clientData));
  288. static void        ListboxComputeGeometry _ANSI_ARGS_((Listbox *listPtr,
  289.                 int fontChanged, int maxIsStale, int updateGrid));
  290. static void        ListboxEventProc _ANSI_ARGS_((ClientData clientData,
  291.                 XEvent *eventPtr));
  292. static int        ListboxFetchSelection _ANSI_ARGS_((
  293.                 ClientData clientData, int offset, char *buffer,
  294.                 int maxBytes));
  295. static void        ListboxLostSelection _ANSI_ARGS_((
  296.                 ClientData clientData));
  297. static void        ListboxRedrawRange _ANSI_ARGS_((Listbox *listPtr,
  298.                 int first, int last));
  299. static void        ListboxScanTo _ANSI_ARGS_((Listbox *listPtr,
  300.                 int x, int y));
  301. static void        ListboxSelect _ANSI_ARGS_((Listbox *listPtr,
  302.                 int first, int last, int select));
  303. static void        ListboxUpdateHScrollbar _ANSI_ARGS_((Listbox *listPtr));
  304. static void        ListboxUpdateVScrollbar _ANSI_ARGS_((Listbox *listPtr));
  305. static int        ListboxWidgetCmd _ANSI_ARGS_((ClientData clientData,
  306.                 Tcl_Interp *interp, int argc, char **argv));
  307. static int        NearestListboxElement _ANSI_ARGS_((Listbox *listPtr,
  308.                 int y));
  309.  
  310. /*
  311.  *--------------------------------------------------------------
  312.  *
  313.  * Tk_ListboxCmd --
  314.  *
  315.  *    This procedure is invoked to process the "listbox" Tcl
  316.  *    command.  See the user documentation for details on what
  317.  *    it does.
  318.  *
  319.  * Results:
  320.  *    A standard Tcl result.
  321.  *
  322.  * Side effects:
  323.  *    See the user documentation.
  324.  *
  325.  *--------------------------------------------------------------
  326.  */
  327.  
  328. int
  329. Tk_ListboxCmd(clientData, interp, argc, argv)
  330.     ClientData clientData;    /* Main window associated with
  331.                  * interpreter. */
  332.     Tcl_Interp *interp;        /* Current interpreter. */
  333.     int argc;            /* Number of arguments. */
  334.     char **argv;        /* Argument strings. */
  335. {
  336.     register Listbox *listPtr;
  337.     Tk_Window new;
  338.     Tk_Window tkwin = (Tk_Window) clientData;
  339.  
  340.     if (argc < 2) {
  341.     Tcl_AppendResult(interp, "wrong # args: should be \"",
  342.         argv[0], " pathName ?options?\"", (char *) NULL);
  343.     return TCL_ERROR;
  344.     }
  345.  
  346.     new = Tk_CreateWindowFromPath(interp, tkwin, argv[1], (char *) NULL);
  347.     if (new == NULL) {
  348.     return TCL_ERROR;
  349.     }
  350.  
  351.     /*
  352.      * Initialize the fields of the structure that won't be initialized
  353.      * by ConfigureListbox, or that ConfigureListbox requires to be
  354.      * initialized already (e.g. resource pointers).
  355.      */
  356.  
  357.     listPtr = (Listbox *) ckalloc(sizeof(Listbox));
  358.     listPtr->tkwin = new;
  359.     listPtr->display = Tk_Display(new);
  360.     listPtr->interp = interp;
  361.     listPtr->widgetCmd = Tcl_CreateCommand(interp,
  362.         Tk_PathName(listPtr->tkwin), ListboxWidgetCmd,
  363.         (ClientData) listPtr, ListboxCmdDeletedProc);
  364.     listPtr->numElements = 0;
  365.     listPtr->firstPtr = NULL;
  366.     listPtr->lastPtr = NULL;
  367.     listPtr->normalBorder = NULL;
  368.     listPtr->borderWidth = 0;
  369.     listPtr->relief = TK_RELIEF_RAISED;
  370.     listPtr->highlightWidth = 0;
  371.     listPtr->highlightBgColorPtr = NULL;
  372.     listPtr->highlightColorPtr = NULL;
  373.     listPtr->inset = 0;
  374.     listPtr->fontPtr = NULL;
  375.     listPtr->fgColorPtr = NULL;
  376.     listPtr->textGC = None;
  377.     listPtr->selBorder = NULL;
  378.     listPtr->selBorderWidth = 0;
  379.     listPtr->selFgColorPtr = None;
  380.     listPtr->selTextGC = None;
  381.     listPtr->width = 0;
  382.     listPtr->height = 0;
  383.     listPtr->lineHeight = 0;
  384.     listPtr->topIndex = 0;
  385.     listPtr->fullLines = 1;
  386.     listPtr->partialLine = 0;
  387.     listPtr->setGrid = 0;
  388.     listPtr->maxWidth = 0;
  389.     listPtr->xScrollUnit = 0;
  390.     listPtr->xOffset = 0;
  391.     listPtr->selectMode = NULL;
  392.     listPtr->numSelected = 0;
  393.     listPtr->selectAnchor = 0;
  394.     listPtr->exportSelection = 1;
  395.     listPtr->active = 0;
  396.     listPtr->scanMarkX = 0;
  397.     listPtr->scanMarkY = 0;
  398.     listPtr->scanMarkXOffset = 0;
  399.     listPtr->scanMarkYIndex = 0;
  400.     listPtr->cursor = None;
  401.     listPtr->takeFocus = NULL;
  402.     listPtr->xScrollCmd = NULL;
  403.     listPtr->yScrollCmd = NULL;
  404.     listPtr->flags = 0;
  405.  
  406.     Tk_SetClass(listPtr->tkwin, "Listbox");
  407.     Tk_CreateEventHandler(listPtr->tkwin,
  408.         ExposureMask|StructureNotifyMask|FocusChangeMask,
  409.         ListboxEventProc, (ClientData) listPtr);
  410.     Tk_CreateSelHandler(listPtr->tkwin, XA_PRIMARY, XA_STRING,
  411.         ListboxFetchSelection, (ClientData) listPtr, XA_STRING);
  412.     if (ConfigureListbox(interp, listPtr, argc-2, argv+2, 0) != TCL_OK) {
  413.     goto error;
  414.     }
  415.  
  416.     interp->result = Tk_PathName(listPtr->tkwin);
  417.     return TCL_OK;
  418.  
  419.     error:
  420.     Tk_DestroyWindow(listPtr->tkwin);
  421.     return TCL_ERROR;
  422. }
  423.  
  424. /*
  425.  *--------------------------------------------------------------
  426.  *
  427.  * ListboxWidgetCmd --
  428.  *
  429.  *    This procedure is invoked to process the Tcl command
  430.  *    that corresponds to a widget managed by this module.
  431.  *    See the user documentation for details on what it does.
  432.  *
  433.  * Results:
  434.  *    A standard Tcl result.
  435.  *
  436.  * Side effects:
  437.  *    See the user documentation.
  438.  *
  439.  *--------------------------------------------------------------
  440.  */
  441.  
  442. static int
  443. ListboxWidgetCmd(clientData, interp, argc, argv)
  444.     ClientData clientData;        /* Information about listbox widget. */
  445.     Tcl_Interp *interp;            /* Current interpreter. */
  446.     int argc;                /* Number of arguments. */
  447.     char **argv;            /* Argument strings. */
  448. {
  449.     register Listbox *listPtr = (Listbox *) clientData;
  450.     int result = TCL_OK;
  451.     size_t length;
  452.     int c;
  453.  
  454.     if (argc < 2) {
  455.     Tcl_AppendResult(interp, "wrong # args: should be \"",
  456.         argv[0], " option ?arg arg ...?\"", (char *) NULL);
  457.     return TCL_ERROR;
  458.     }
  459.     Tk_Preserve((ClientData) listPtr);
  460.     c = argv[1][0];
  461.     length = strlen(argv[1]);
  462.     if ((c == 'a') && (strncmp(argv[1], "activate", length) == 0)) {
  463.     int index;
  464.  
  465.     if (argc != 3) {
  466.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  467.             argv[0], " activate index\"",
  468.             (char *) NULL);
  469.         goto error;
  470.     }
  471.     ListboxRedrawRange(listPtr, listPtr->active, listPtr->active);
  472.     if (GetListboxIndex(interp, listPtr, argv[2], 0, &index)
  473.         != TCL_OK) {
  474.         goto error;
  475.     }
  476.     listPtr->active = index;
  477.     ListboxRedrawRange(listPtr, listPtr->active, listPtr->active);
  478.     } else if ((c == 'b') && (strncmp(argv[1], "bbox", length) == 0)) {
  479.     int index, x, y, i;
  480.     Element *elPtr;
  481.  
  482.     if (argc != 3) {
  483.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  484.             argv[0], " bbox index\"", (char *) NULL);
  485.         goto error;
  486.     }
  487.     if (GetListboxIndex(interp, listPtr, argv[2], 0, &index) != TCL_OK) {
  488.         goto error;
  489.     }
  490.     for (i = 0, elPtr = listPtr->firstPtr; i < index;
  491.         i++, elPtr = elPtr->nextPtr) {
  492.         /* Empty loop body. */
  493.     }
  494.     if ((index >= listPtr->topIndex)
  495.             && (index < (listPtr->topIndex + listPtr->fullLines
  496.             + listPtr->partialLine))) {
  497.         x = listPtr->inset - listPtr->xOffset;
  498.         y = ((index - listPtr->topIndex)*listPtr->lineHeight)
  499.             + listPtr->inset + listPtr->selBorderWidth;
  500.         sprintf(interp->result, "%d %d %d %d", x, y, elPtr->pixelWidth,
  501.             listPtr->fontPtr->ascent + listPtr->fontPtr->descent);
  502.     }
  503.     } else if ((c == 'c') && (strncmp(argv[1], "cget", length) == 0)
  504.         && (length >= 2)) {
  505.     if (argc != 3) {
  506.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  507.             argv[0], " cget option\"",
  508.             (char *) NULL);
  509.         goto error;
  510.     }
  511.     result = Tk_ConfigureValue(interp, listPtr->tkwin, configSpecs,
  512.         (char *) listPtr, argv[2], 0);
  513.     } else if ((c == 'c') && (strncmp(argv[1], "configure", length) == 0)
  514.         && (length >= 2)) {
  515.     if (argc == 2) {
  516.         result = Tk_ConfigureInfo(interp, listPtr->tkwin, configSpecs,
  517.             (char *) listPtr, (char *) NULL, 0);
  518.     } else if (argc == 3) {
  519.         result = Tk_ConfigureInfo(interp, listPtr->tkwin, configSpecs,
  520.             (char *) listPtr, argv[2], 0);
  521.     } else {
  522.         result = ConfigureListbox(interp, listPtr, argc-2, argv+2,
  523.             TK_CONFIG_ARGV_ONLY);
  524.     }
  525.     } else if ((c == 'c') && (strncmp(argv[1], "curselection", length) == 0)
  526.         && (length >= 2)) {
  527.     int i, count;
  528.     char index[20];
  529.     Element *elPtr;
  530.  
  531.     if (argc != 2) {
  532.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  533.             argv[0], " curselection\"",
  534.             (char *) NULL);
  535.         goto error;
  536.     }
  537.     count = 0;
  538.     for (i = 0, elPtr = listPtr->firstPtr; elPtr != NULL;
  539.         i++, elPtr = elPtr->nextPtr) {
  540.         if (elPtr->selected) {
  541.         sprintf(index, "%d", i);
  542.         Tcl_AppendElement(interp, index);
  543.         count++;
  544.         }
  545.     }
  546.     if (count != listPtr->numSelected) {
  547.         panic("ListboxWidgetCmd: selection count incorrect");
  548.     }
  549.     } else if ((c == 'd') && (strncmp(argv[1], "delete", length) == 0)) {
  550.     int first, last;
  551.  
  552.     if ((argc < 3) || (argc > 4)) {
  553.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  554.             argv[0], " delete firstIndex ?lastIndex?\"",
  555.             (char *) NULL);
  556.         goto error;
  557.     }
  558.     if (GetListboxIndex(interp, listPtr, argv[2], 0, &first) != TCL_OK) {
  559.         goto error;
  560.     }
  561.     if (argc == 3) {
  562.         last = first;
  563.     } else {
  564.         if (GetListboxIndex(interp, listPtr, argv[3], 0, &last) != TCL_OK) {
  565.         goto error;
  566.         }
  567.     }
  568.     DeleteEls(listPtr, first, last);
  569.     } else if ((c == 'g') && (strncmp(argv[1], "get", length) == 0)) {
  570.     int first, last, i;
  571.     Element *elPtr;
  572.  
  573.     if ((argc != 3) && (argc != 4)) {
  574.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  575.             argv[0], " get first ?last?\"", (char *) NULL);
  576.         goto error;
  577.     }
  578.     if (GetListboxIndex(interp, listPtr, argv[2], 0, &first) != TCL_OK) {
  579.         goto error;
  580.     }
  581.     if ((argc == 4) && (GetListboxIndex(interp, listPtr, argv[3],
  582.         0, &last) != TCL_OK)) {
  583.         goto error;
  584.     }
  585.     for (elPtr = listPtr->firstPtr, i = 0; i < first;
  586.         i++, elPtr = elPtr->nextPtr) {
  587.         /* Empty loop body. */
  588.     }
  589.     if (elPtr != NULL) {
  590.         if (argc == 3) {
  591.         interp->result = elPtr->text;
  592.         } else {
  593.         for (  ; i <= last; i++, elPtr = elPtr->nextPtr) {
  594.             Tcl_AppendElement(interp, elPtr->text);
  595.         }
  596.         }
  597.     }
  598.     } else if ((c == 'i') && (strncmp(argv[1], "index", length) == 0)
  599.         && (length >= 3)) {
  600.     int index;
  601.  
  602.     if (argc != 3) {
  603.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  604.             argv[0], " index index\"",
  605.             (char *) NULL);
  606.         goto error;
  607.     }
  608.     if (GetListboxIndex(interp, listPtr, argv[2], 1, &index)
  609.         != TCL_OK) {
  610.         goto error;
  611.     }
  612.     sprintf(interp->result, "%d", index);
  613.     } else if ((c == 'i') && (strncmp(argv[1], "insert", length) == 0)
  614.         && (length >= 3)) {
  615.     int index;
  616.  
  617.     if (argc < 3) {
  618.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  619.             argv[0], " insert index ?element element ...?\"",
  620.             (char *) NULL);
  621.         goto error;
  622.     }
  623.     if (GetListboxIndex(interp, listPtr, argv[2], 1, &index)
  624.         != TCL_OK) {
  625.         goto error;
  626.     }
  627.     InsertEls(listPtr, index, argc-3, argv+3);
  628.     } else if ((c == 'n') && (strncmp(argv[1], "nearest", length) == 0)) {
  629.     int index, y;
  630.  
  631.     if (argc != 3) {
  632.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  633.             argv[0], " nearest y\"", (char *) NULL);
  634.         goto error;
  635.     }
  636.     if (Tcl_GetInt(interp, argv[2], &y) != TCL_OK) {
  637.         goto error;
  638.     }
  639.     index = NearestListboxElement(listPtr, y);
  640.     sprintf(interp->result, "%d", index);
  641.     } else if ((c == 's') && (length >= 2)
  642.         && (strncmp(argv[1], "scan", length) == 0)) {
  643.     int x, y;
  644.  
  645.     if (argc != 5) {
  646.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  647.             argv[0], " scan mark|dragto x y\"", (char *) NULL);
  648.         goto error;
  649.     }
  650.     if ((Tcl_GetInt(interp, argv[3], &x) != TCL_OK)
  651.         || (Tcl_GetInt(interp, argv[4], &y) != TCL_OK)) {
  652.         goto error;
  653.     }
  654.     if ((argv[2][0] == 'm')
  655.         && (strncmp(argv[2], "mark", strlen(argv[2])) == 0)) {
  656.         listPtr->scanMarkX = x;
  657.         listPtr->scanMarkY = y;
  658.         listPtr->scanMarkXOffset = listPtr->xOffset;
  659.         listPtr->scanMarkYIndex = listPtr->topIndex;
  660.     } else if ((argv[2][0] == 'd')
  661.         && (strncmp(argv[2], "dragto", strlen(argv[2])) == 0)) {
  662.         ListboxScanTo(listPtr, x, y);
  663.     } else {
  664.         Tcl_AppendResult(interp, "bad scan option \"", argv[2],
  665.             "\":  must be mark or dragto", (char *) NULL);
  666.         goto error;
  667.     }
  668.     } else if ((c == 's') && (strncmp(argv[1], "see", length) == 0)
  669.         && (length >= 3)) {
  670.     int index, diff;
  671.     if (argc != 3) {
  672.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  673.             argv[0], " see index\"",
  674.             (char *) NULL);
  675.         goto error;
  676.     }
  677.     if (GetListboxIndex(interp, listPtr, argv[2], 0, &index) != TCL_OK) {
  678.         goto error;
  679.     }
  680.     diff = listPtr->topIndex-index;
  681.     if (diff > 0) {
  682.         if (diff <= (listPtr->fullLines/3)) {
  683.         ChangeListboxView(listPtr, index);
  684.         } else {
  685.         ChangeListboxView(listPtr, index - (listPtr->fullLines-1)/2);
  686.         }
  687.     } else {
  688.         diff = index - (listPtr->topIndex + listPtr->fullLines - 1);
  689.         if (diff > 0) {
  690.         if (diff <= (listPtr->fullLines/3)) {
  691.             ChangeListboxView(listPtr, listPtr->topIndex + diff);
  692.         } else {
  693.             ChangeListboxView(listPtr,
  694.                 index - (listPtr->fullLines-1)/2);
  695.         }
  696.         }
  697.     }
  698.     } else if ((c == 's') && (length >= 3)
  699.         && (strncmp(argv[1], "selection", length) == 0)) {
  700.     int first, last;
  701.  
  702.     if ((argc != 4) && (argc != 5)) {
  703.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  704.             argv[0], " selection option index ?index?\"",
  705.             (char *) NULL);
  706.         goto error;
  707.     }
  708.     if (GetListboxIndex(interp, listPtr, argv[3], 0, &first) != TCL_OK) {
  709.         goto error;
  710.     }
  711.     if (argc == 5) {
  712.         if (GetListboxIndex(interp, listPtr, argv[4], 0, &last) != TCL_OK) {
  713.         goto error;
  714.         }
  715.     } else {
  716.         last = first;
  717.     }
  718.     length = strlen(argv[2]);
  719.     c = argv[2][0];
  720.     if ((c == 'a') && (strncmp(argv[2], "anchor", length) == 0)) {
  721.         if (argc != 4) {
  722.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  723.             argv[0], " selection anchor index\"", (char *) NULL);
  724.         goto error;
  725.         }
  726.         listPtr->selectAnchor = first;
  727.     } else if ((c == 'c') && (strncmp(argv[2], "clear", length) == 0)) {
  728.         ListboxSelect(listPtr, first, last, 0);
  729.     } else if ((c == 'i') && (strncmp(argv[2], "includes", length) == 0)) {
  730.         int i;
  731.         Element *elPtr;
  732.     
  733.         if (argc != 4) {
  734.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  735.             argv[0], " selection includes index\"", (char *) NULL);
  736.         goto error;
  737.         }
  738.         for (elPtr = listPtr->firstPtr, i = 0; i < first;
  739.             i++, elPtr = elPtr->nextPtr) {
  740.         /* Empty loop body. */
  741.         }
  742.         if ((elPtr != NULL) && (elPtr->selected)) {
  743.         interp->result = "1";
  744.         } else {
  745.         interp->result = "0";
  746.         }
  747.     } else if ((c == 's') && (strncmp(argv[2], "set", length) == 0)) {
  748.         ListboxSelect(listPtr, first, last, 1);
  749.     } else {
  750.         Tcl_AppendResult(interp, "bad selection option \"", argv[2],
  751.             "\": must be anchor, clear, includes, or set",
  752.             (char *) NULL);
  753.         goto error;
  754.     }
  755.     } else if ((c == 's') && (length >= 2)
  756.         && (strncmp(argv[1], "size", length) == 0)) {
  757.     if (argc != 2) {
  758.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  759.             argv[0], " size\"", (char *) NULL);
  760.         goto error;
  761.     }
  762.     sprintf(interp->result, "%d", listPtr->numElements);
  763.     } else if ((c == 'x') && (strncmp(argv[1], "xview", length) == 0)) {
  764.     int index, count, type, windowWidth, windowUnits;
  765.     int offset = 0;        /* Initialized to stop gcc warnings. */
  766.     double fraction, fraction2;
  767.  
  768.     windowWidth = Tk_Width(listPtr->tkwin)
  769.         - 2*(listPtr->inset + listPtr->selBorderWidth);
  770.     if (argc == 2) {
  771.         if (listPtr->maxWidth == 0) {
  772.         interp->result = "0 1";
  773.         } else {
  774.         fraction = listPtr->xOffset/((double) listPtr->maxWidth);
  775.         fraction2 = (listPtr->xOffset + windowWidth)
  776.             /((double) listPtr->maxWidth);
  777.         if (fraction2 > 1.0) {
  778.             fraction2 = 1.0;
  779.         }
  780.         sprintf(interp->result, "%g %g", fraction, fraction2);
  781.         }
  782.     } else if (argc == 3) {
  783.         if (Tcl_GetInt(interp, argv[2], &index) != TCL_OK) {
  784.         goto error;
  785.         }
  786.         ChangeListboxOffset(listPtr, index*listPtr->xScrollUnit);
  787.     } else {
  788.         type = Tk_GetScrollInfo(interp, argc, argv, &fraction, &count);
  789.         switch (type) {
  790.         case TK_SCROLL_ERROR:
  791.             goto error;
  792.         case TK_SCROLL_MOVETO:
  793.             offset = fraction*listPtr->maxWidth;
  794.             break;
  795.         case TK_SCROLL_PAGES:
  796.             windowUnits = windowWidth/listPtr->xScrollUnit;
  797.             if (windowUnits > 2) {
  798.             offset = listPtr->xOffset
  799.                 + count*listPtr->xScrollUnit*(windowUnits-2);
  800.             } else {
  801.             offset = listPtr->xOffset + count*listPtr->xScrollUnit;
  802.             }
  803.             break;
  804.         case TK_SCROLL_UNITS:
  805.             offset = listPtr->xOffset + count*listPtr->xScrollUnit;
  806.             break;
  807.         }
  808.         ChangeListboxOffset(listPtr, offset);
  809.     }
  810.     } else if ((c == 'y') && (strncmp(argv[1], "yview", length) == 0)) {
  811.     int index, count, type;
  812.     double fraction, fraction2;
  813.  
  814.     if (argc == 2) {
  815.         if (listPtr->numElements == 0) {
  816.         interp->result = "0 1";
  817.         } else {
  818.         fraction = listPtr->topIndex/((double) listPtr->numElements);
  819.         fraction2 = (listPtr->topIndex+listPtr->fullLines)
  820.             /((double) listPtr->numElements);
  821.         if (fraction2 > 1.0) {
  822.             fraction2 = 1.0;
  823.         }
  824.         sprintf(interp->result, "%g %g", fraction, fraction2);
  825.         }
  826.     } else if (argc == 3) {
  827.         if (GetListboxIndex(interp, listPtr, argv[2], 0, &index)
  828.             != TCL_OK) {
  829.         goto error;
  830.         }
  831.         ChangeListboxView(listPtr, index);
  832.     } else {
  833.         type = Tk_GetScrollInfo(interp, argc, argv, &fraction, &count);
  834.         switch (type) {
  835.         case TK_SCROLL_ERROR:
  836.             goto error;
  837.         case TK_SCROLL_MOVETO:
  838.             index = listPtr->numElements*fraction;
  839.             break;
  840.         case TK_SCROLL_PAGES:
  841.             if (listPtr->fullLines > 2) {
  842.             index = listPtr->topIndex
  843.                 + count*(listPtr->fullLines-2);
  844.             } else {
  845.             index = listPtr->topIndex + count;
  846.             }
  847.             break;
  848.         case TK_SCROLL_UNITS:
  849.             index = listPtr->topIndex + count;
  850.             break;
  851.         }
  852.         ChangeListboxView(listPtr, index);
  853.     }
  854.     } else {
  855.     Tcl_AppendResult(interp, "bad option \"", argv[1],
  856.         "\": must be activate, bbox, cget, configure, ",
  857.         "curselection, delete, get, index, insert, nearest, ",
  858.         "scan, see, selection, size, ",
  859.         "xview, or yview", (char *) NULL);
  860.     goto error;
  861.     }
  862.     Tk_Release((ClientData) listPtr);
  863.     return result;
  864.  
  865.     error:
  866.     Tk_Release((ClientData) listPtr);
  867.     return TCL_ERROR;
  868. }
  869.  
  870. /*
  871.  *----------------------------------------------------------------------
  872.  *
  873.  * DestroyListbox --
  874.  *
  875.  *    This procedure is invoked by Tk_EventuallyFree or Tk_Release
  876.  *    to clean up the internal structure of a listbox at a safe time
  877.  *    (when no-one is using it anymore).
  878.  *
  879.  * Results:
  880.  *    None.
  881.  *
  882.  * Side effects:
  883.  *    Everything associated with the listbox is freed up.
  884.  *
  885.  *----------------------------------------------------------------------
  886.  */
  887.  
  888. static void
  889. DestroyListbox(clientData)
  890.     ClientData clientData;    /* Info about listbox widget. */
  891. {
  892.     register Listbox *listPtr = (Listbox *) clientData;
  893.     register Element *elPtr, *nextPtr;
  894.  
  895.     /*
  896.      * Free up all of the list elements.
  897.      */
  898.  
  899.     for (elPtr = listPtr->firstPtr; elPtr != NULL; ) {
  900.     nextPtr = elPtr->nextPtr;
  901.     ckfree((char *) elPtr);
  902.     elPtr = nextPtr;
  903.     }
  904.  
  905.     /*
  906.      * Free up all the stuff that requires special handling, then
  907.      * let Tk_FreeOptions handle all the standard option-related
  908.      * stuff.
  909.      */
  910.  
  911.     if (listPtr->textGC != None) {
  912.     Tk_FreeGC(listPtr->display, listPtr->textGC);
  913.     }
  914.     if (listPtr->selTextGC != None) {
  915.     Tk_FreeGC(listPtr->display, listPtr->selTextGC);
  916.     }
  917.     Tk_FreeOptions(configSpecs, (char *) listPtr, listPtr->display, 0);
  918.     ckfree((char *) listPtr);
  919. }
  920.  
  921. /*
  922.  *----------------------------------------------------------------------
  923.  *
  924.  * ConfigureListbox --
  925.  *
  926.  *    This procedure is called to process an argv/argc list, plus
  927.  *    the Tk option database, in order to configure (or reconfigure)
  928.  *    a listbox widget.
  929.  *
  930.  * Results:
  931.  *    The return value is a standard Tcl result.  If TCL_ERROR is
  932.  *    returned, then interp->result contains an error message.
  933.  *
  934.  * Side effects:
  935.  *    Configuration information, such as colors, border width,
  936.  *    etc. get set for listPtr;  old resources get freed,
  937.  *    if there were any.
  938.  *
  939.  *----------------------------------------------------------------------
  940.  */
  941.  
  942. static int
  943. ConfigureListbox(interp, listPtr, argc, argv, flags)
  944.     Tcl_Interp *interp;        /* Used for error reporting. */
  945.     register Listbox *listPtr;    /* Information about widget;  may or may
  946.                  * not already have values for some fields. */
  947.     int argc;            /* Number of valid entries in argv. */
  948.     char **argv;        /* Arguments. */
  949.     int flags;            /* Flags to pass to Tk_ConfigureWidget. */
  950. {
  951.     XGCValues gcValues;
  952.     GC new;
  953.     int oldExport;
  954.  
  955.     oldExport = listPtr->exportSelection;
  956.     if (Tk_ConfigureWidget(interp, listPtr->tkwin, configSpecs,
  957.         argc, argv, (char *) listPtr, flags) != TCL_OK) {
  958.     return TCL_ERROR;
  959.     }
  960.  
  961.     /*
  962.      * A few options need special processing, such as setting the
  963.      * background from a 3-D border.
  964.      */
  965.  
  966.     Tk_SetBackgroundFromBorder(listPtr->tkwin, listPtr->normalBorder);
  967.  
  968.     if (listPtr->highlightWidth < 0) {
  969.     listPtr->highlightWidth = 0;
  970.     }
  971.     listPtr->inset = listPtr->highlightWidth + listPtr->borderWidth;
  972.  
  973.     gcValues.foreground = listPtr->fgColorPtr->pixel;
  974.     gcValues.font = listPtr->fontPtr->fid;
  975.     gcValues.graphics_exposures = False;
  976.     new = Tk_GetGC(listPtr->tkwin, GCForeground|GCFont|GCGraphicsExposures,
  977.         &gcValues);
  978.     if (listPtr->textGC != None) {
  979.     Tk_FreeGC(listPtr->display, listPtr->textGC);
  980.     }
  981.     listPtr->textGC = new;
  982.  
  983.     gcValues.foreground = listPtr->selFgColorPtr->pixel;
  984.     gcValues.font = listPtr->fontPtr->fid;
  985.     new = Tk_GetGC(listPtr->tkwin, GCForeground|GCFont, &gcValues);
  986.     if (listPtr->selTextGC != None) {
  987.     Tk_FreeGC(listPtr->display, listPtr->selTextGC);
  988.     }
  989.     listPtr->selTextGC = new;
  990.  
  991.     /*
  992.      * Claim the selection if we've suddenly started exporting it and
  993.      * there is a selection to export.
  994.      */
  995.  
  996.     if (listPtr->exportSelection && !oldExport
  997.         && (listPtr->numSelected != 0)) {
  998.     Tk_OwnSelection(listPtr->tkwin, XA_PRIMARY, ListboxLostSelection,
  999.         (ClientData) listPtr);
  1000.     }
  1001.  
  1002.     /*
  1003.      * Register the desired geometry for the window and arrange for
  1004.      * the window to be redisplayed.
  1005.      */
  1006.  
  1007.     ListboxComputeGeometry(listPtr, 1, 1, 1);
  1008.     listPtr->flags |= UPDATE_V_SCROLLBAR|UPDATE_H_SCROLLBAR;
  1009.     ListboxRedrawRange(listPtr, 0, listPtr->numElements-1);
  1010.     return TCL_OK;
  1011. }
  1012.  
  1013. /*
  1014.  *--------------------------------------------------------------
  1015.  *
  1016.  * DisplayListbox --
  1017.  *
  1018.  *    This procedure redraws the contents of a listbox window.
  1019.  *
  1020.  * Results:
  1021.  *    None.
  1022.  *
  1023.  * Side effects:
  1024.  *    Information appears on the screen.
  1025.  *
  1026.  *--------------------------------------------------------------
  1027.  */
  1028.  
  1029. static void
  1030. DisplayListbox(clientData)
  1031.     ClientData clientData;    /* Information about window. */
  1032. {
  1033.     register Listbox *listPtr = (Listbox *) clientData;
  1034.     register Tk_Window tkwin = listPtr->tkwin;
  1035.     register Element *elPtr;
  1036.     GC gc;
  1037.     int i, limit, x, y, width, prevSelected;
  1038.     int left, right;            /* Non-zero values here indicate
  1039.                      * that the left or right edge of
  1040.                      * the listbox is off-screen. */
  1041.     Pixmap pixmap;
  1042.  
  1043.     listPtr->flags &= ~REDRAW_PENDING;
  1044.     if (listPtr->flags & UPDATE_V_SCROLLBAR) {
  1045.     ListboxUpdateVScrollbar(listPtr);
  1046.     }
  1047.     if (listPtr->flags & UPDATE_H_SCROLLBAR) {
  1048.     ListboxUpdateHScrollbar(listPtr);
  1049.     }
  1050.     listPtr->flags &= ~(REDRAW_PENDING|UPDATE_V_SCROLLBAR|UPDATE_H_SCROLLBAR);
  1051.     if ((listPtr->tkwin == NULL) || !Tk_IsMapped(tkwin)) {
  1052.     return;
  1053.     }
  1054.  
  1055.     /*
  1056.      * Redrawing is done in a temporary pixmap that is allocated
  1057.      * here and freed at the end of the procedure.  All drawing is
  1058.      * done to the pixmap, and the pixmap is copied to the screen
  1059.      * at the end of the procedure.  This provides the smoothest
  1060.      * possible visual effects (no flashing on the screen).
  1061.      */
  1062.  
  1063.     pixmap = Tk_GetPixmap(listPtr->display, Tk_WindowId(tkwin),
  1064.         Tk_Width(tkwin), Tk_Height(tkwin), Tk_Depth(tkwin));
  1065.     Tk_Fill3DRectangle(tkwin, pixmap, listPtr->normalBorder, 0, 0,
  1066.         Tk_Width(tkwin), Tk_Height(tkwin), 0, TK_RELIEF_FLAT);
  1067.  
  1068.     /*
  1069.      * Iterate through all of the elements of the listbox, displaying each
  1070.      * in turn.  Selected elements use a different GC and have a raised
  1071.      * background.
  1072.      */
  1073.  
  1074.     limit = listPtr->topIndex + listPtr->fullLines + listPtr->partialLine - 1;
  1075.     if (limit >= listPtr->numElements) {
  1076.     limit = listPtr->numElements-1;
  1077.     }
  1078.     left = right = 0;
  1079.     if (listPtr->xOffset > 0) {
  1080.     left = listPtr->selBorderWidth+1;
  1081.     }
  1082.     if ((listPtr->maxWidth - listPtr->xOffset) > (Tk_Width(listPtr->tkwin)
  1083.         - 2*(listPtr->inset + listPtr->selBorderWidth)))  {
  1084.     right = listPtr->selBorderWidth+1;
  1085.     }
  1086.     prevSelected = 0;
  1087.     for (elPtr = listPtr->firstPtr, i = 0; (elPtr != NULL) && (i <= limit);
  1088.         prevSelected = elPtr->selected, elPtr = elPtr->nextPtr, i++) {
  1089.     if (i < listPtr->topIndex) {
  1090.         continue;
  1091.     }
  1092.     x = listPtr->inset;
  1093.     y = ((i - listPtr->topIndex) * listPtr->lineHeight) 
  1094.         + listPtr->inset;
  1095.     gc = listPtr->textGC;
  1096.     if (elPtr->selected) {
  1097.         gc = listPtr->selTextGC;
  1098.         width = Tk_Width(tkwin) - 2*listPtr->inset;
  1099.         Tk_Fill3DRectangle(tkwin, pixmap, listPtr->selBorder, x, y,
  1100.             width, listPtr->lineHeight, 0, TK_RELIEF_FLAT);
  1101.  
  1102.         /*
  1103.          * Draw beveled edges around the selection, if there are visible
  1104.          * edges next to this element.  Special considerations:
  1105.          * 1. The left and right bevels may not be visible if horizontal
  1106.          *    scrolling is enabled (the "left" and "right" variables
  1107.          *    are zero to indicate that the corresponding bevel is
  1108.          *    visible).
  1109.          * 2. Top and bottom bevels are only drawn if this is the
  1110.          *    first or last seleted item.
  1111.          * 3. If the left or right bevel isn't visible, then the "left"
  1112.          *    and "right" variables, computed above, have non-zero values
  1113.          *    that extend the top and bottom bevels so that the mitered
  1114.          *    corners are off-screen.
  1115.          */
  1116.  
  1117.         if (left == 0) {
  1118.         Tk_3DVerticalBevel(tkwin, pixmap, listPtr->selBorder,
  1119.             x, y, listPtr->selBorderWidth, listPtr->lineHeight,
  1120.             1, TK_RELIEF_RAISED);
  1121.         }
  1122.         if (right == 0) {
  1123.         Tk_3DVerticalBevel(tkwin, pixmap, listPtr->selBorder,
  1124.             x + width - listPtr->selBorderWidth, y,
  1125.             listPtr->selBorderWidth, listPtr->lineHeight,
  1126.             0, TK_RELIEF_RAISED);
  1127.         }
  1128.         if (!prevSelected) {
  1129.         Tk_3DHorizontalBevel(tkwin, pixmap, listPtr->selBorder,
  1130.             x-left, y, width+left+right, listPtr->selBorderWidth,
  1131.             1, 1, 1, TK_RELIEF_RAISED);
  1132.         }
  1133.         if ((elPtr->nextPtr == NULL) || !elPtr->nextPtr->selected) {
  1134.         Tk_3DHorizontalBevel(tkwin, pixmap, listPtr->selBorder, x-left,
  1135.             y + listPtr->lineHeight - listPtr->selBorderWidth,
  1136.             width+left+right, listPtr->selBorderWidth, 0, 0, 0,
  1137.             TK_RELIEF_RAISED);
  1138.         }
  1139.     }
  1140.     y += listPtr->fontPtr->ascent + listPtr->selBorderWidth;
  1141.     x = listPtr->inset + listPtr->selBorderWidth - elPtr->lBearing
  1142.         - listPtr->xOffset;
  1143.     XDrawString(listPtr->display, pixmap, gc, x, y,
  1144.         elPtr->text, elPtr->textLength);
  1145.  
  1146.     /*
  1147.      * If this is the active element, underline it.
  1148.      */
  1149.  
  1150.     if ((i == listPtr->active) && (listPtr->flags & GOT_FOCUS)) {
  1151.         XFillRectangle(listPtr->display, pixmap, gc,
  1152.             listPtr->inset + listPtr->selBorderWidth
  1153.             - listPtr->xOffset,
  1154.             y + listPtr->fontPtr->descent - 1,
  1155.             (unsigned) elPtr->pixelWidth, 1);
  1156.     }
  1157.     }
  1158.  
  1159.     /*
  1160.      * Redraw the border for the listbox to make sure that it's on top
  1161.      * of any of the text of the listbox entries.
  1162.      */
  1163.  
  1164.     Tk_Draw3DRectangle(tkwin, pixmap, listPtr->normalBorder,
  1165.         listPtr->highlightWidth, listPtr->highlightWidth,
  1166.         Tk_Width(tkwin) - 2*listPtr->highlightWidth,
  1167.         Tk_Height(tkwin) - 2*listPtr->highlightWidth,
  1168.         listPtr->borderWidth, listPtr->relief);
  1169.     if (listPtr->highlightWidth > 0) {
  1170.     GC gc;
  1171.  
  1172.     if (listPtr->flags & GOT_FOCUS) {
  1173.         gc = Tk_GCForColor(listPtr->highlightColorPtr, pixmap);
  1174.     } else {
  1175.         gc = Tk_GCForColor(listPtr->highlightBgColorPtr, pixmap);
  1176.     }
  1177.     Tk_DrawFocusHighlight(tkwin, gc, listPtr->highlightWidth, pixmap);
  1178.     }
  1179.     XCopyArea(listPtr->display, pixmap, Tk_WindowId(tkwin),
  1180.         listPtr->textGC, 0, 0, (unsigned) Tk_Width(tkwin),
  1181.         (unsigned) Tk_Height(tkwin), 0, 0);
  1182.     Tk_FreePixmap(listPtr->display, pixmap);
  1183. }
  1184.  
  1185. /*
  1186.  *----------------------------------------------------------------------
  1187.  *
  1188.  * ListboxComputeGeometry --
  1189.  *
  1190.  *    This procedure is invoked to recompute geometry information
  1191.  *    such as the sizes of the elements and the overall dimensions
  1192.  *    desired for the listbox.
  1193.  *
  1194.  * Results:
  1195.  *    None.
  1196.  *
  1197.  * Side effects:
  1198.  *    Geometry information is updated and a new requested size is
  1199.  *    registered for the widget.  Internal border and gridding
  1200.  *    information is also set.
  1201.  *
  1202.  *----------------------------------------------------------------------
  1203.  */
  1204.  
  1205. static void
  1206. ListboxComputeGeometry(listPtr, fontChanged, maxIsStale, updateGrid)
  1207.     Listbox *listPtr;        /* Listbox whose geometry is to be
  1208.                  * recomputed. */
  1209.     int fontChanged;        /* Non-zero means the font may have changed
  1210.                  * so per-element width information also
  1211.                  * has to be computed. */
  1212.     int maxIsStale;        /* Non-zero means the "maxWidth" field may
  1213.                  * no longer be up-to-date and must
  1214.                  * be recomputed.  If fontChanged is 1 then
  1215.                  * this must be 1. */
  1216.     int updateGrid;        /* Non-zero means call Tk_SetGrid or
  1217.                  * Tk_UnsetGrid to update gridding for
  1218.                  * the window. */
  1219. {
  1220.     register Element *elPtr;
  1221.     int dummy, fontHeight, width, height, pixelWidth, pixelHeight;
  1222.     XCharStruct bbox;
  1223.  
  1224.     if (fontChanged  || maxIsStale) {
  1225.     listPtr->xScrollUnit = XTextWidth(listPtr->fontPtr, "0", 1);
  1226.     listPtr->maxWidth = 0;
  1227.     for (elPtr = listPtr->firstPtr; elPtr != NULL; elPtr = elPtr->nextPtr) {
  1228.         if (fontChanged) {
  1229.         XTextExtents(listPtr->fontPtr, elPtr->text, elPtr->textLength,
  1230.             &dummy, &dummy, &dummy, &bbox);
  1231.         elPtr->lBearing = bbox.lbearing;
  1232.         elPtr->pixelWidth = bbox.rbearing - bbox.lbearing;
  1233.         }
  1234.         if (elPtr->pixelWidth > listPtr->maxWidth) {
  1235.         listPtr->maxWidth = elPtr->pixelWidth;
  1236.         }
  1237.     }
  1238.     }
  1239.  
  1240.     fontHeight = listPtr->fontPtr->ascent + listPtr->fontPtr->descent;
  1241.     listPtr->lineHeight = fontHeight + 1 + 2*listPtr->selBorderWidth;
  1242.     width = listPtr->width;
  1243.     if (width <= 0) {
  1244.     width = (listPtr->maxWidth + listPtr->xScrollUnit - 1)
  1245.         /listPtr->xScrollUnit;
  1246.     if (width < 1) {
  1247.         width = 1;
  1248.     }
  1249.     }
  1250.     pixelWidth = width*listPtr->xScrollUnit + 2*listPtr->inset
  1251.         + 2*listPtr->selBorderWidth;
  1252.     height = listPtr->height;
  1253.     if (listPtr->height <= 0) {
  1254.     height = listPtr->numElements;
  1255.     if (height < 1) {
  1256.         height = 1;
  1257.     }
  1258.     }
  1259.     pixelHeight = height*listPtr->lineHeight + 2*listPtr->inset;
  1260.     Tk_GeometryRequest(listPtr->tkwin, pixelWidth, pixelHeight);
  1261.     Tk_SetInternalBorder(listPtr->tkwin, listPtr->inset);
  1262.     if (updateGrid) {
  1263.     if (listPtr->setGrid) {
  1264.         Tk_SetGrid(listPtr->tkwin, width, height, listPtr->xScrollUnit,
  1265.             listPtr->lineHeight);
  1266.     } else {
  1267.         Tk_UnsetGrid(listPtr->tkwin);
  1268.     }
  1269.     }
  1270. }
  1271.  
  1272. /*
  1273.  *----------------------------------------------------------------------
  1274.  *
  1275.  * InsertEls --
  1276.  *
  1277.  *    Add new elements to a listbox widget.
  1278.  *
  1279.  * Results:
  1280.  *    None.
  1281.  *
  1282.  * Side effects:
  1283.  *    New information gets added to listPtr;  it will be redisplayed
  1284.  *    soon, but not immediately.
  1285.  *
  1286.  *----------------------------------------------------------------------
  1287.  */
  1288.  
  1289. static void
  1290. InsertEls(listPtr, index, argc, argv)
  1291.     register Listbox *listPtr;    /* Listbox that is to get the new
  1292.                  * elements. */
  1293.     int index;            /* Add the new elements before this
  1294.                  * element. */
  1295.     int argc;            /* Number of new elements to add. */
  1296.     char **argv;        /* New elements (one per entry). */
  1297. {
  1298.     register Element *prevPtr, *newPtr;
  1299.     int length, dummy, i, oldMaxWidth;
  1300.     XCharStruct bbox;
  1301.  
  1302.     /*
  1303.      * Find the element before which the new ones will be inserted.
  1304.      */
  1305.  
  1306.     if (index <= 0) {
  1307.     index = 0;
  1308.     }
  1309.     if (index > listPtr->numElements) {
  1310.     index = listPtr->numElements;
  1311.     }
  1312.     if (index == 0) {
  1313.     prevPtr = NULL;
  1314.     } else if (index == listPtr->numElements) {
  1315.           prevPtr = listPtr->lastPtr;
  1316.     } else {
  1317.     for (prevPtr = listPtr->firstPtr, i = index - 1; i > 0; i--) {
  1318.         prevPtr = prevPtr->nextPtr;
  1319.     }
  1320.     }
  1321.  
  1322.     /*
  1323.      * For each new element, create a record, initialize it, and link
  1324.      * it into the list of elements.
  1325.      */
  1326.  
  1327.     oldMaxWidth = listPtr->maxWidth;
  1328.     for (i = argc ; i > 0; i--, argv++, prevPtr = newPtr) {
  1329.     length = strlen(*argv);
  1330.     newPtr = (Element *) ckalloc(ElementSize(length));
  1331.     newPtr->textLength = length;
  1332.     strcpy(newPtr->text, *argv);
  1333.     XTextExtents(listPtr->fontPtr, newPtr->text, newPtr->textLength,
  1334.         &dummy, &dummy, &dummy, &bbox);
  1335.     newPtr->lBearing = bbox.lbearing;
  1336.     newPtr->pixelWidth = bbox.rbearing - bbox.lbearing;
  1337.     if (newPtr->pixelWidth > listPtr->maxWidth) {
  1338.         listPtr->maxWidth = newPtr->pixelWidth;
  1339.     }
  1340.     newPtr->selected = 0;
  1341.     if (prevPtr == NULL) {
  1342.         newPtr->nextPtr = listPtr->firstPtr;
  1343.         listPtr->firstPtr = newPtr;
  1344.     } else {
  1345.         newPtr->nextPtr = prevPtr->nextPtr;
  1346.         prevPtr->nextPtr = newPtr;
  1347.     }
  1348.     }
  1349.     if ((prevPtr != NULL) && (prevPtr->nextPtr == NULL)) {
  1350.     listPtr->lastPtr = prevPtr;
  1351.     }
  1352.     listPtr->numElements += argc;
  1353.  
  1354.     /*
  1355.      * Update the selection and other indexes to account for the
  1356.      * renumbering that has just occurred.  Then arrange for the new
  1357.      * information to be displayed.
  1358.      */
  1359.  
  1360.     if (index <= listPtr->selectAnchor) {
  1361.     listPtr->selectAnchor += argc;
  1362.     }
  1363.     if (index < listPtr->topIndex) {
  1364.     listPtr->topIndex += argc;
  1365.     }
  1366.     if (index <= listPtr->active) {
  1367.     listPtr->active += argc;
  1368.     if ((listPtr->active >= listPtr->numElements)
  1369.         && (listPtr->numElements > 0)) {
  1370.         listPtr->active = listPtr->numElements-1;
  1371.     }
  1372.     }
  1373.     listPtr->flags |= UPDATE_V_SCROLLBAR;
  1374.     if (listPtr->maxWidth != oldMaxWidth) {
  1375.     listPtr->flags |= UPDATE_H_SCROLLBAR;
  1376.     }
  1377.     ListboxComputeGeometry(listPtr, 0, 0, 0);
  1378.     ListboxRedrawRange(listPtr, index, listPtr->numElements-1);
  1379. }
  1380.  
  1381. /*
  1382.  *----------------------------------------------------------------------
  1383.  *
  1384.  * DeleteEls --
  1385.  *
  1386.  *    Remove one or more elements from a listbox widget.
  1387.  *
  1388.  * Results:
  1389.  *    None.
  1390.  *
  1391.  * Side effects:
  1392.  *    Memory gets freed, the listbox gets modified and (eventually)
  1393.  *    redisplayed.
  1394.  *
  1395.  *----------------------------------------------------------------------
  1396.  */
  1397.  
  1398. static void
  1399. DeleteEls(listPtr, first, last)
  1400.     register Listbox *listPtr;    /* Listbox widget to modify. */
  1401.     int first;            /* Index of first element to delete. */
  1402.     int last;            /* Index of last element to delete. */
  1403. {
  1404.     register Element *prevPtr, *elPtr;
  1405.     int count, i, widthChanged;
  1406.  
  1407.     /*
  1408.      * Adjust the range to fit within the existing elements of the
  1409.      * listbox, and make sure there's something to delete.
  1410.      */
  1411.  
  1412.     if (first < 0) {
  1413.     first = 0;
  1414.     }
  1415.     if (last >= listPtr->numElements) {
  1416.     last = listPtr->numElements-1;
  1417.     }
  1418.     count = last + 1 - first;
  1419.     if (count <= 0) {
  1420.     return;
  1421.     }
  1422.  
  1423.     /*
  1424.      * Find the element just before the ones to delete.
  1425.      */
  1426.  
  1427.     if (first == 0) {
  1428.     prevPtr = NULL;
  1429.     } else {
  1430.     for (i = first-1, prevPtr = listPtr->firstPtr; i > 0; i--) {
  1431.         prevPtr = prevPtr->nextPtr;
  1432.     }
  1433.     }
  1434.  
  1435.     /*
  1436.      * Delete the requested number of elements.
  1437.      */
  1438.  
  1439.     widthChanged = 0;
  1440.     for (i = count; i > 0; i--) {
  1441.     if (prevPtr == NULL) {
  1442.         elPtr = listPtr->firstPtr;
  1443.         listPtr->firstPtr = elPtr->nextPtr;
  1444.         if (listPtr->firstPtr == NULL) {
  1445.         listPtr->lastPtr = NULL;
  1446.         }
  1447.     } else {
  1448.         elPtr = prevPtr->nextPtr;
  1449.         prevPtr->nextPtr = elPtr->nextPtr;
  1450.         if (prevPtr->nextPtr == NULL) {
  1451.         listPtr->lastPtr = prevPtr;
  1452.         }
  1453.     }
  1454.     if (elPtr->pixelWidth == listPtr->maxWidth) {
  1455.         widthChanged = 1;
  1456.     }
  1457.     if (elPtr->selected) {
  1458.         listPtr->numSelected -= 1;
  1459.     }
  1460.     ckfree((char *) elPtr);
  1461.     }
  1462.     listPtr->numElements -= count;
  1463.  
  1464.     /*
  1465.      * Update the selection and viewing information to reflect the change
  1466.      * in the element numbering, and redisplay to slide information up over
  1467.      * the elements that were deleted.
  1468.      */
  1469.  
  1470.     if (first <= listPtr->selectAnchor) {
  1471.     listPtr->selectAnchor -= count;
  1472.     if (listPtr->selectAnchor < first) {
  1473.         listPtr->selectAnchor = first;
  1474.     }
  1475.     }
  1476.     if (first <= listPtr->topIndex) {
  1477.     listPtr->topIndex -= count;
  1478.     if (listPtr->topIndex < first) {
  1479.         listPtr->topIndex = first;
  1480.     }
  1481.     }
  1482.     if (listPtr->topIndex > (listPtr->numElements - listPtr->fullLines)) {
  1483.     listPtr->topIndex = listPtr->numElements - listPtr->fullLines;
  1484.     if (listPtr->topIndex < 0) {
  1485.         listPtr->topIndex = 0;
  1486.     }
  1487.     }
  1488.     if (listPtr->active > last) {
  1489.     listPtr->active -= count;
  1490.     } else if (listPtr->active >= first) {
  1491.     listPtr->active = first;
  1492.     if ((listPtr->active >= listPtr->numElements)
  1493.         && (listPtr->numElements > 0)) {
  1494.         listPtr->active = listPtr->numElements-1;
  1495.     }
  1496.     }
  1497.     listPtr->flags |= UPDATE_V_SCROLLBAR;
  1498.     ListboxComputeGeometry(listPtr, 0, widthChanged, 0);
  1499.     if (widthChanged) {
  1500.     listPtr->flags |= UPDATE_H_SCROLLBAR;
  1501.     }
  1502.     ListboxRedrawRange(listPtr, first, listPtr->numElements-1);
  1503. }
  1504.  
  1505. /*
  1506.  *--------------------------------------------------------------
  1507.  *
  1508.  * ListboxEventProc --
  1509.  *
  1510.  *    This procedure is invoked by the Tk dispatcher for various
  1511.  *    events on listboxes.
  1512.  *
  1513.  * Results:
  1514.  *    None.
  1515.  *
  1516.  * Side effects:
  1517.  *    When the window gets deleted, internal structures get
  1518.  *    cleaned up.  When it gets exposed, it is redisplayed.
  1519.  *
  1520.  *--------------------------------------------------------------
  1521.  */
  1522.  
  1523. static void
  1524. ListboxEventProc(clientData, eventPtr)
  1525.     ClientData clientData;    /* Information about window. */
  1526.     XEvent *eventPtr;        /* Information about event. */
  1527. {
  1528.     Listbox *listPtr = (Listbox *) clientData;
  1529.  
  1530.     if (eventPtr->type == Expose) {
  1531.     ListboxRedrawRange(listPtr,
  1532.         NearestListboxElement(listPtr, eventPtr->xexpose.y),
  1533.         NearestListboxElement(listPtr, eventPtr->xexpose.y
  1534.         + eventPtr->xexpose.height));
  1535.     } else if (eventPtr->type == DestroyNotify) {
  1536.     if (listPtr->setGrid) {
  1537.         Tk_UnsetGrid(listPtr->tkwin);
  1538.     }
  1539.     if (listPtr->tkwin != NULL) {
  1540.         listPtr->tkwin = NULL;
  1541.         Tcl_DeleteCommand(listPtr->interp,
  1542.             Tcl_GetCommandName(listPtr->interp, listPtr->widgetCmd));
  1543.     }
  1544.     if (listPtr->flags & REDRAW_PENDING) {
  1545.         Tk_CancelIdleCall(DisplayListbox, (ClientData) listPtr);
  1546.     }
  1547.     Tk_EventuallyFree((ClientData) listPtr, DestroyListbox);
  1548.     } else if (eventPtr->type == ConfigureNotify) {
  1549.     int vertSpace;
  1550.  
  1551.     vertSpace = Tk_Height(listPtr->tkwin) - 2*listPtr->inset;
  1552.     listPtr->fullLines = vertSpace / listPtr->lineHeight;
  1553.     if ((listPtr->fullLines*listPtr->lineHeight) < vertSpace) {
  1554.         listPtr->partialLine = 1;
  1555.     } else {
  1556.         listPtr->partialLine = 0;
  1557.     }
  1558.     listPtr->flags |= UPDATE_V_SCROLLBAR|UPDATE_H_SCROLLBAR;
  1559.     ChangeListboxView(listPtr, listPtr->topIndex);
  1560.     ChangeListboxOffset(listPtr, listPtr->xOffset);
  1561.  
  1562.     /*
  1563.      * Redraw the whole listbox.  It's hard to tell what needs
  1564.      * to be redrawn (e.g. if the listbox has shrunk then we
  1565.      * may only need to redraw the borders), so just redraw
  1566.      * everything for safety.
  1567.      */
  1568.  
  1569.     ListboxRedrawRange(listPtr, 0, listPtr->numElements-1);
  1570.     } else if (eventPtr->type == FocusIn) {
  1571.     if (eventPtr->xfocus.detail != NotifyInferior) {
  1572.         listPtr->flags |= GOT_FOCUS;
  1573.         ListboxRedrawRange(listPtr, 0, listPtr->numElements-1);
  1574.     }
  1575.     } else if (eventPtr->type == FocusOut) {
  1576.     if (eventPtr->xfocus.detail != NotifyInferior) {
  1577.         listPtr->flags &= ~GOT_FOCUS;
  1578.         ListboxRedrawRange(listPtr, 0, listPtr->numElements-1);
  1579.     }
  1580.     }
  1581. }
  1582.  
  1583. /*
  1584.  *----------------------------------------------------------------------
  1585.  *
  1586.  * ListboxCmdDeletedProc --
  1587.  *
  1588.  *    This procedure is invoked when a widget command is deleted.  If
  1589.  *    the widget isn't already in the process of being destroyed,
  1590.  *    this command destroys it.
  1591.  *
  1592.  * Results:
  1593.  *    None.
  1594.  *
  1595.  * Side effects:
  1596.  *    The widget is destroyed.
  1597.  *
  1598.  *----------------------------------------------------------------------
  1599.  */
  1600.  
  1601. static void
  1602. ListboxCmdDeletedProc(clientData)
  1603.     ClientData clientData;    /* Pointer to widget record for widget. */
  1604. {
  1605.     Listbox *listPtr = (Listbox *) clientData;
  1606.     Tk_Window tkwin = listPtr->tkwin;
  1607.  
  1608.     /*
  1609.      * This procedure could be invoked either because the window was
  1610.      * destroyed and the command was then deleted (in which case tkwin
  1611.      * is NULL) or because the command was deleted, and then this procedure
  1612.      * destroys the widget.
  1613.      */
  1614.  
  1615.     if (tkwin != NULL) {
  1616.     listPtr->tkwin = NULL;
  1617.     Tk_DestroyWindow(tkwin);
  1618.     }
  1619. }
  1620.  
  1621. /*
  1622.  *--------------------------------------------------------------
  1623.  *
  1624.  * GetListboxIndex --
  1625.  *
  1626.  *    Parse an index into a listbox and return either its value
  1627.  *    or an error.
  1628.  *
  1629.  * Results:
  1630.  *    A standard Tcl result.  If all went well, then *indexPtr is
  1631.  *    filled in with the index (into listPtr) corresponding to
  1632.  *    string.  Otherwise an error message is left in interp->result.
  1633.  *
  1634.  * Side effects:
  1635.  *    None.
  1636.  *
  1637.  *--------------------------------------------------------------
  1638.  */
  1639.  
  1640. static int
  1641. GetListboxIndex(interp, listPtr, string, numElsOK, indexPtr)
  1642.     Tcl_Interp *interp;        /* For error messages. */
  1643.     Listbox *listPtr;        /* Listbox for which the index is being
  1644.                  * specified. */
  1645.     char *string;        /* Specifies an element in the listbox. */
  1646.     int numElsOK;        /* 0 means the return value must be less
  1647.                  * less than the number of entries in
  1648.                  * the listbox;  1 means it may also be
  1649.                  * equal to the number of entries. */
  1650.     int *indexPtr;        /* Where to store converted index. */
  1651. {
  1652.     int c;
  1653.     size_t length;
  1654.  
  1655.     length = strlen(string);
  1656.     c = string[0];
  1657.     if ((c == 'a') && (strncmp(string, "active", length) == 0)
  1658.         && (length >= 2)) {
  1659.     *indexPtr = listPtr->active;
  1660.     } else if ((c == 'a') && (strncmp(string, "anchor", length) == 0)
  1661.         && (length >= 2)) {
  1662.     *indexPtr = listPtr->selectAnchor;
  1663.     } else if ((c == 'e') && (strncmp(string, "end", length) == 0)) {
  1664.     *indexPtr = listPtr->numElements;
  1665.     } else if (c == '@') {
  1666.     int x, y;
  1667.     char *p, *end;
  1668.  
  1669.     p = string+1;
  1670.     x = strtol(p, &end, 0);
  1671.     if ((end == p) || (*end != ',')) {
  1672.         goto badIndex;
  1673.     }
  1674.     p = end+1;
  1675.     y = strtol(p, &end, 0);
  1676.     if ((end == p) || (*end != 0)) {
  1677.         goto badIndex;
  1678.     }
  1679.     *indexPtr = NearestListboxElement(listPtr, y);
  1680.     } else {
  1681.     if (Tcl_GetInt(interp, string, indexPtr) != TCL_OK) {
  1682.         Tcl_ResetResult(interp);
  1683.         goto badIndex;
  1684.     }
  1685.     }
  1686.     if (numElsOK) {
  1687.     if (*indexPtr > listPtr->numElements) {
  1688.         *indexPtr = listPtr->numElements;
  1689.     }
  1690.     } else if (*indexPtr >= listPtr->numElements) {
  1691.     *indexPtr = listPtr->numElements-1;
  1692.     }
  1693.     if (*indexPtr < 0) {
  1694.     *indexPtr = 0;
  1695.     }
  1696.     return TCL_OK;
  1697.  
  1698.     badIndex:
  1699.     Tcl_AppendResult(interp, "bad listbox index \"", string,
  1700.         "\":  must be active, anchor, end, @x,y, or a number",
  1701.         (char *) NULL);
  1702.     return TCL_ERROR;
  1703. }
  1704.  
  1705. /*
  1706.  *----------------------------------------------------------------------
  1707.  *
  1708.  * ChangeListboxView --
  1709.  *
  1710.  *    Change the view on a listbox widget so that a given element
  1711.  *    is displayed at the top.
  1712.  *
  1713.  * Results:
  1714.  *    None.
  1715.  *
  1716.  * Side effects:
  1717.  *    What's displayed on the screen is changed.  If there is a
  1718.  *    scrollbar associated with this widget, then the scrollbar
  1719.  *    is instructed to change its display too.
  1720.  *
  1721.  *----------------------------------------------------------------------
  1722.  */
  1723.  
  1724. static void
  1725. ChangeListboxView(listPtr, index)
  1726.     register Listbox *listPtr;        /* Information about widget. */
  1727.     int index;                /* Index of element in listPtr
  1728.                      * that should now appear at the
  1729.                      * top of the listbox. */
  1730. {
  1731.     if (index >= (listPtr->numElements - listPtr->fullLines)) {
  1732.     index = listPtr->numElements - listPtr->fullLines;
  1733.     }
  1734.     if (index < 0) {
  1735.     index = 0;
  1736.     }
  1737.     if (listPtr->topIndex != index) {
  1738.     listPtr->topIndex = index;
  1739.     if (!(listPtr->flags & REDRAW_PENDING)) {
  1740.         Tk_DoWhenIdle(DisplayListbox, (ClientData) listPtr);
  1741.         listPtr->flags |= REDRAW_PENDING;
  1742.     }
  1743.     listPtr->flags |= UPDATE_V_SCROLLBAR;
  1744.     }
  1745. }
  1746.  
  1747. /*
  1748.  *----------------------------------------------------------------------
  1749.  *
  1750.  * ChangListboxOffset --
  1751.  *
  1752.  *    Change the horizontal offset for a listbox.
  1753.  *
  1754.  * Results:
  1755.  *    None.
  1756.  *
  1757.  * Side effects:
  1758.  *    The listbox may be redrawn to reflect its new horizontal
  1759.  *    offset.
  1760.  *
  1761.  *----------------------------------------------------------------------
  1762.  */
  1763.  
  1764. static void
  1765. ChangeListboxOffset(listPtr, offset)
  1766.     register Listbox *listPtr;        /* Information about widget. */
  1767.     int offset;                /* Desired new "xOffset" for
  1768.                      * listbox. */
  1769. {
  1770.     int maxOffset;
  1771.  
  1772.     /*
  1773.      * Make sure that the new offset is within the allowable range, and
  1774.      * round it off to an even multiple of xScrollUnit.
  1775.      */
  1776.  
  1777.     maxOffset = listPtr->maxWidth + (listPtr->xScrollUnit-1)
  1778.         - (Tk_Width(listPtr->tkwin) - 2*listPtr->inset
  1779.         - 2*listPtr->selBorderWidth - listPtr->xScrollUnit);
  1780.     if (offset > maxOffset) {
  1781.     offset = maxOffset;
  1782.     }
  1783.     if (offset < 0) {
  1784.     offset = 0;
  1785.     }
  1786.     offset -= offset%listPtr->xScrollUnit;
  1787.     if (offset != listPtr->xOffset) {
  1788.     listPtr->xOffset = offset;
  1789.     listPtr->flags |= UPDATE_H_SCROLLBAR;
  1790.     ListboxRedrawRange(listPtr, 0, listPtr->numElements);
  1791.     }
  1792. }
  1793.  
  1794. /*
  1795.  *----------------------------------------------------------------------
  1796.  *
  1797.  * ListboxScanTo --
  1798.  *
  1799.  *    Given a point (presumably of the curent mouse location)
  1800.  *    drag the view in the window to implement the scan operation.
  1801.  *
  1802.  * Results:
  1803.  *    None.
  1804.  *
  1805.  * Side effects:
  1806.  *    The view in the window may change.
  1807.  *
  1808.  *----------------------------------------------------------------------
  1809.  */
  1810.  
  1811. static void
  1812. ListboxScanTo(listPtr, x, y)
  1813.     register Listbox *listPtr;        /* Information about widget. */
  1814.     int x;                /* X-coordinate to use for scan
  1815.                      * operation. */
  1816.     int y;                /* Y-coordinate to use for scan
  1817.                      * operation. */
  1818. {
  1819.     int newTopIndex, newOffset, maxIndex, maxOffset;
  1820.  
  1821.     maxIndex = listPtr->numElements - listPtr->fullLines;
  1822.     maxOffset = listPtr->maxWidth + (listPtr->xScrollUnit-1)
  1823.         - (Tk_Width(listPtr->tkwin) - 2*listPtr->inset
  1824.         - 2*listPtr->selBorderWidth - listPtr->xScrollUnit);
  1825.  
  1826.     /*
  1827.      * Compute new top line for screen by amplifying the difference
  1828.      * between the current position and the place where the scan
  1829.      * started (the "mark" position).  If we run off the top or bottom
  1830.      * of the list, then reset the mark point so that the current
  1831.      * position continues to correspond to the edge of the window.
  1832.      * This means that the picture will start dragging as soon as the
  1833.      * mouse reverses direction (without this reset, might have to slide
  1834.      * mouse a long ways back before the picture starts moving again).
  1835.      */
  1836.  
  1837.     newTopIndex = listPtr->scanMarkYIndex
  1838.         - (10*(y - listPtr->scanMarkY))/listPtr->lineHeight;
  1839.     if (newTopIndex > maxIndex) {
  1840.     newTopIndex = listPtr->scanMarkYIndex = maxIndex;
  1841.     listPtr->scanMarkY = y;
  1842.     } else if (newTopIndex < 0) {
  1843.     newTopIndex = listPtr->scanMarkYIndex = 0;
  1844.     listPtr->scanMarkY = y;
  1845.     }
  1846.     ChangeListboxView(listPtr, newTopIndex);
  1847.  
  1848.     /*
  1849.      * Compute new left edge for display in a similar fashion by amplifying
  1850.      * the difference between the current position and the place where the
  1851.      * scan started.
  1852.      */
  1853.  
  1854.     newOffset = listPtr->scanMarkXOffset - (10*(x - listPtr->scanMarkX));
  1855.     if (newOffset > maxOffset) {
  1856.     newOffset = listPtr->scanMarkXOffset = maxOffset;
  1857.     listPtr->scanMarkX = x;
  1858.     } else if (newOffset < 0) {
  1859.     newOffset = listPtr->scanMarkXOffset = 0;
  1860.     listPtr->scanMarkX = x;
  1861.     }
  1862.     ChangeListboxOffset(listPtr, newOffset);
  1863. }
  1864.  
  1865. /*
  1866.  *----------------------------------------------------------------------
  1867.  *
  1868.  * NearestListboxElement --
  1869.  *
  1870.  *    Given a y-coordinate inside a listbox, compute the index of
  1871.  *    the element under that y-coordinate (or closest to that
  1872.  *    y-coordinate).
  1873.  *
  1874.  * Results:
  1875.  *    The return value is an index of an element of listPtr.  If
  1876.  *    listPtr has no elements, then 0 is always returned.
  1877.  *
  1878.  * Side effects:
  1879.  *    None.
  1880.  *
  1881.  *----------------------------------------------------------------------
  1882.  */
  1883.  
  1884. static int
  1885. NearestListboxElement(listPtr, y)
  1886.     register Listbox *listPtr;        /* Information about widget. */
  1887.     int y;                /* Y-coordinate in listPtr's window. */
  1888. {
  1889.     int index;
  1890.  
  1891.     index = (y - listPtr->inset)/listPtr->lineHeight;
  1892.     if (index >= (listPtr->fullLines + listPtr->partialLine)) {
  1893.     index = listPtr->fullLines + listPtr->partialLine - 1;
  1894.     }
  1895.     if (index < 0) {
  1896.     index = 0;
  1897.     }
  1898.     index += listPtr->topIndex;
  1899.     if (index >= listPtr->numElements) {
  1900.     index = listPtr->numElements-1;
  1901.     }
  1902.     return index;
  1903. }
  1904.  
  1905. /*
  1906.  *----------------------------------------------------------------------
  1907.  *
  1908.  * ListboxSelect --
  1909.  *
  1910.  *    Select or deselect one or more elements in a listbox..
  1911.  *
  1912.  * Results:
  1913.  *    None.
  1914.  *
  1915.  * Side effects:
  1916.  *    All of the elements in the range between first and last are
  1917.  *    marked as either selected or deselected, depending on the
  1918.  *    "select" argument.  Any items whose state changes are redisplayed.
  1919.  *    The selection is claimed from X when the number of selected
  1920.  *    elements changes from zero to non-zero.
  1921.  *
  1922.  *----------------------------------------------------------------------
  1923.  */
  1924.  
  1925. static void
  1926. ListboxSelect(listPtr, first, last, select)
  1927.     register Listbox *listPtr;        /* Information about widget. */
  1928.     int first;                /* Index of first element to
  1929.                      * select or deselect. */
  1930.     int last;                /* Index of last element to
  1931.                      * select or deselect. */
  1932.     int select;                /* 1 means select items, 0 means
  1933.                      * deselect them. */
  1934. {
  1935.     int i, firstRedisplay, lastRedisplay, increment, oldCount;
  1936.     Element *elPtr;
  1937.  
  1938.     if (last < first) {
  1939.     i = first;
  1940.     first = last;
  1941.     last = i;
  1942.     }
  1943.     if (first >= listPtr->numElements) {
  1944.     return;
  1945.     }
  1946.     oldCount = listPtr->numSelected;
  1947.     firstRedisplay = -1;
  1948.     increment = select ? 1 : -1;
  1949.     for (i = 0, elPtr = listPtr->firstPtr; i < first;
  1950.         i++, elPtr = elPtr->nextPtr) {
  1951.     /* Empty loop body. */
  1952.     }
  1953.     for ( ; i <= last; i++, elPtr = elPtr->nextPtr) {
  1954.     if (elPtr->selected == select) {
  1955.         continue;
  1956.     }
  1957.     listPtr->numSelected += increment;
  1958.     elPtr->selected = select;
  1959.     if (firstRedisplay < 0) {
  1960.         firstRedisplay = i;
  1961.     }
  1962.     lastRedisplay = i;
  1963.     }
  1964.     if (firstRedisplay >= 0) {
  1965.     ListboxRedrawRange(listPtr, first, last);
  1966.     }
  1967.     if ((oldCount == 0) && (listPtr->numSelected > 0)
  1968.         && (listPtr->exportSelection)) {
  1969.     Tk_OwnSelection(listPtr->tkwin, XA_PRIMARY, ListboxLostSelection,
  1970.         (ClientData) listPtr);
  1971.     }
  1972. }
  1973.  
  1974. /*
  1975.  *----------------------------------------------------------------------
  1976.  *
  1977.  * ListboxFetchSelection --
  1978.  *
  1979.  *    This procedure is called back by Tk when the selection is
  1980.  *    requested by someone.  It returns part or all of the selection
  1981.  *    in a buffer provided by the caller.
  1982.  *
  1983.  * Results:
  1984.  *    The return value is the number of non-NULL bytes stored
  1985.  *    at buffer.  Buffer is filled (or partially filled) with a
  1986.  *    NULL-terminated string containing part or all of the selection,
  1987.  *    as given by offset and maxBytes.  The selection is returned
  1988.  *    as a Tcl list with one list element for each element in the
  1989.  *    listbox.
  1990.  *
  1991.  * Side effects:
  1992.  *    None.
  1993.  *
  1994.  *----------------------------------------------------------------------
  1995.  */
  1996.  
  1997. static int
  1998. ListboxFetchSelection(clientData, offset, buffer, maxBytes)
  1999.     ClientData clientData;        /* Information about listbox widget. */
  2000.     int offset;                /* Offset within selection of first
  2001.                      * byte to be returned. */
  2002.     char *buffer;            /* Location in which to place
  2003.                      * selection. */
  2004.     int maxBytes;            /* Maximum number of bytes to place
  2005.                      * at buffer, not including terminating
  2006.                      * NULL character. */
  2007. {
  2008.     register Listbox *listPtr = (Listbox *) clientData;
  2009.     register Element *elPtr;
  2010.     Tcl_DString selection;
  2011.     int length, count, needNewline;
  2012.  
  2013.     if (!listPtr->exportSelection) {
  2014.     return -1;
  2015.     }
  2016.  
  2017.     /*
  2018.      * Use a dynamic string to accumulate the contents of the selection.
  2019.      */
  2020.  
  2021.     needNewline = 0;
  2022.     Tcl_DStringInit(&selection);
  2023.     for (elPtr = listPtr->firstPtr; elPtr != NULL; elPtr = elPtr->nextPtr) {
  2024.     if (elPtr->selected) {
  2025.         if (needNewline) {
  2026.         Tcl_DStringAppend(&selection, "\n", 1);
  2027.         }
  2028.         Tcl_DStringAppend(&selection, elPtr->text, elPtr->textLength);
  2029.         needNewline = 1;
  2030.     }
  2031.     }
  2032.  
  2033.     length = Tcl_DStringLength(&selection);
  2034.     if (length == 0) {
  2035.     return -1;
  2036.     }
  2037.  
  2038.     /*
  2039.      * Copy the requested portion of the selection to the buffer.
  2040.      */
  2041.  
  2042.     count = length - offset;
  2043.     if (count <= 0) {
  2044.     count = 0;
  2045.     } else {
  2046.     if (count > maxBytes) {
  2047.         count = maxBytes;
  2048.     }
  2049.     memcpy((VOID *) buffer,
  2050.         (VOID *) (Tcl_DStringValue(&selection) + offset),
  2051.         (size_t) count);
  2052.     }
  2053.     buffer[count] = '\0';
  2054.     Tcl_DStringFree(&selection);
  2055.     return count;
  2056. }
  2057.  
  2058. /*
  2059.  *----------------------------------------------------------------------
  2060.  *
  2061.  * ListboxLostSelection --
  2062.  *
  2063.  *    This procedure is called back by Tk when the selection is
  2064.  *    grabbed away from a listbox widget.
  2065.  *
  2066.  * Results:
  2067.  *    None.
  2068.  *
  2069.  * Side effects:
  2070.  *    The existing selection is unhighlighted, and the window is
  2071.  *    marked as not containing a selection.
  2072.  *
  2073.  *----------------------------------------------------------------------
  2074.  */
  2075.  
  2076. static void
  2077. ListboxLostSelection(clientData)
  2078.     ClientData clientData;        /* Information about listbox widget. */
  2079. {
  2080.     register Listbox *listPtr = (Listbox *) clientData;
  2081.  
  2082.     if ((listPtr->exportSelection) && (listPtr->numElements > 0)) {
  2083.     ListboxSelect(listPtr, 0, listPtr->numElements-1, 0);
  2084.     }
  2085. }
  2086.  
  2087. /*
  2088.  *----------------------------------------------------------------------
  2089.  *
  2090.  * ListboxRedrawRange --
  2091.  *
  2092.  *    Ensure that a given range of elements is eventually redrawn on
  2093.  *    the display (if those elements in fact appear on the display).
  2094.  *
  2095.  * Results:
  2096.  *    None.
  2097.  *
  2098.  * Side effects:
  2099.  *    Information gets redisplayed.
  2100.  *
  2101.  *----------------------------------------------------------------------
  2102.  */
  2103.  
  2104.     /* ARGSUSED */
  2105. static void
  2106. ListboxRedrawRange(listPtr, first, last)
  2107.     register Listbox *listPtr;        /* Information about widget. */
  2108.     int first;                /* Index of first element in list
  2109.                      * that needs to be redrawn. */
  2110.     int last;                /* Index of last element in list
  2111.                      * that needs to be redrawn.  May
  2112.                      * be less than first;
  2113.                      * these just bracket a range. */
  2114. {
  2115.     if ((listPtr->tkwin == NULL) || !Tk_IsMapped(listPtr->tkwin)
  2116.         || (listPtr->flags & REDRAW_PENDING)) {
  2117.     return;
  2118.     }
  2119.     Tk_DoWhenIdle(DisplayListbox, (ClientData) listPtr);
  2120.     listPtr->flags |= REDRAW_PENDING;
  2121. }
  2122.  
  2123. /*
  2124.  *----------------------------------------------------------------------
  2125.  *
  2126.  * ListboxUpdateVScrollbar --
  2127.  *
  2128.  *    This procedure is invoked whenever information has changed in
  2129.  *    a listbox in a way that would invalidate a vertical scrollbar
  2130.  *    display.  If there is an associated scrollbar, then this command
  2131.  *    updates it by invoking a Tcl command.
  2132.  *
  2133.  * Results:
  2134.  *    None.
  2135.  *
  2136.  * Side effects:
  2137.  *    A Tcl command is invoked, and an additional command may be
  2138.  *    invoked to process errors in the command.
  2139.  *
  2140.  *----------------------------------------------------------------------
  2141.  */
  2142.  
  2143. static void
  2144. ListboxUpdateVScrollbar(listPtr)
  2145.     register Listbox *listPtr;        /* Information about widget. */
  2146. {
  2147.     char string[100];
  2148.     double first, last;
  2149.     int result;
  2150.  
  2151.     if (listPtr->yScrollCmd == NULL) {
  2152.     return;
  2153.     }
  2154.     if (listPtr->numElements == 0) {
  2155.     first = 0.0;
  2156.     last = 1.0;
  2157.     } else {
  2158.     first = listPtr->topIndex/((double) listPtr->numElements);
  2159.     last = (listPtr->topIndex+listPtr->fullLines)
  2160.         /((double) listPtr->numElements);
  2161.     if (last > 1.0) {
  2162.         last = 1.0;
  2163.     }
  2164.     }
  2165.     sprintf(string, " %g %g", first, last);
  2166.     result = Tcl_VarEval(listPtr->interp, listPtr->yScrollCmd, string,
  2167.         (char *) NULL);
  2168.     if (result != TCL_OK) {
  2169.     Tcl_AddErrorInfo(listPtr->interp,
  2170.         "\n    (vertical scrolling command executed by listbox)");
  2171.     Tk_BackgroundError(listPtr->interp);
  2172.     }
  2173. }
  2174.  
  2175. /*
  2176.  *----------------------------------------------------------------------
  2177.  *
  2178.  * ListboxUpdateHScrollbar --
  2179.  *
  2180.  *    This procedure is invoked whenever information has changed in
  2181.  *    a listbox in a way that would invalidate a horizontal scrollbar
  2182.  *    display.  If there is an associated horizontal scrollbar, then
  2183.  *    this command updates it by invoking a Tcl command.
  2184.  *
  2185.  * Results:
  2186.  *    None.
  2187.  *
  2188.  * Side effects:
  2189.  *    A Tcl command is invoked, and an additional command may be
  2190.  *    invoked to process errors in the command.
  2191.  *
  2192.  *----------------------------------------------------------------------
  2193.  */
  2194.  
  2195. static void
  2196. ListboxUpdateHScrollbar(listPtr)
  2197.     register Listbox *listPtr;        /* Information about widget. */
  2198. {
  2199.     char string[60];
  2200.     int result, windowWidth;
  2201.     double first, last;
  2202.  
  2203.     if (listPtr->xScrollCmd == NULL) {
  2204.     return;
  2205.     }
  2206.     windowWidth = Tk_Width(listPtr->tkwin) - 2*(listPtr->inset
  2207.         + listPtr->selBorderWidth);
  2208.     if (listPtr->maxWidth == 0) {
  2209.     first = 0;
  2210.     last = 1.0;
  2211.     } else {
  2212.     first = listPtr->xOffset/((double) listPtr->maxWidth);
  2213.     last = (listPtr->xOffset + windowWidth)
  2214.         /((double) listPtr->maxWidth);
  2215.     if (last > 1.0) {
  2216.         last = 1.0;
  2217.     }
  2218.     }
  2219.     sprintf(string, " %g %g", first, last);
  2220.     result = Tcl_VarEval(listPtr->interp, listPtr->xScrollCmd, string,
  2221.         (char *) NULL);
  2222.     if (result != TCL_OK) {
  2223.     Tcl_AddErrorInfo(listPtr->interp,
  2224.         "\n    (horizontal scrolling command executed by listbox)");
  2225.     Tk_BackgroundError(listPtr->interp);
  2226.     }
  2227. }
  2228.